package com.zr.d_c.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zr.d_c.dao.BookDao;
import com.zr.d_c.daoimpl.BookDaoImpl;
import com.zr.d_c.service.BookService;
import com.zr.d_c.serviceimpl.BookServiceImpl;

/**
 * 添加到购物车controller
 * 
 * @author 王朝
 *
 */
public class AddDishToBuycartController extends HttpServlet {

	BookService bs = new BookServiceImpl();
    BookDao bd = new BookDaoImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		PrintWriter pw = resp.getWriter();
		int uid = Integer.parseInt(req.getParameter("uid"));
		int did = Integer.parseInt(req.getParameter("did"));
		int bnum = Integer.parseInt(req.getParameter("bnum"));
		int oldbnum = bd.selectBuycartDishByDid(did);
		int flag = 0;
		if (oldbnum > 0) {
			int totalbnum = bnum + oldbnum;
			flag = bs.modDishInByucart(did, totalbnum);
		}else{
			flag = bs.addDishToBuycart(did, uid, bnum);
		}
		pw.write(String.valueOf(flag));
	}
}
